package net.bezrabotnih.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import java.util.List;
import net.bezrabotnih.client.managed.request.ApplicationRequestFactory;
import net.bezrabotnih.client.managed.request.JobTypeProxy;
import net.bezrabotnih.client.managed.request.ThumbnailProxy;
import net.bezrabotnih.client.managed.request.UserAccountProxy;
import net.bezrabotnih.client.managed.request.UserAccountRequest;
import net.bezrabotnih.client.managed.request.UserRatingProxy;
import net.bezrabotnih.client.managed.ui.JobTypeListEditor;
import net.bezrabotnih.client.managed.ui.UserAccountDetailsView;
import net.bezrabotnih.client.managed.ui.UserAccountEditView;
import net.bezrabotnih.client.managed.ui.UserAccountListView;
import net.bezrabotnih.client.managed.ui.UserAccountMobileDetailsView;
import net.bezrabotnih.client.managed.ui.UserAccountMobileEditView;
import net.bezrabotnih.client.managed.ui.UserRatingListEditor;
import net.bezrabotnih.client.scaffold.ScaffoldApp;
import net.bezrabotnih.client.scaffold.place.CreateAndEditProxy;
import net.bezrabotnih.client.scaffold.place.FindAndEditProxy;
import net.bezrabotnih.client.scaffold.place.ProxyPlace;

public class UserAccountActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public UserAccountActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new UserAccountDetailsActivity((EntityProxyId<UserAccountProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? UserAccountMobileDetailsView.instance() : UserAccountDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<net.bezrabotnih.client.managed.request.UserAccountProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<UserAccountProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        UserAccountEditView.instance().setCreating(true);
        final UserAccountRequest request = requests.userAccountRequest();
        Activity activity = new CreateAndEditProxy<UserAccountProxy>(UserAccountProxy.class, request, ScaffoldApp.isMobile() ? UserAccountMobileEditView.instance() : UserAccountEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(UserAccountProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new UserAccountEditActivityWrapper(requests, ScaffoldApp.isMobile() ? UserAccountMobileEditView.instance() : UserAccountEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        UserAccountEditView.instance().setCreating(false);
        EntityProxyId<UserAccountProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<UserAccountProxy>(proxyId, requests, ScaffoldApp.isMobile() ? UserAccountMobileEditView.instance() : UserAccountEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(UserAccountProxy proxy) {
                UserAccountRequest request = requests.userAccountRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new UserAccountEditActivityWrapper(requests, ScaffoldApp.isMobile() ? UserAccountMobileEditView.instance() : UserAccountEditView.instance(), activity, proxyId);
    }
}
